package com.leyou.service.receive;

import com.leyou.api.ReceiveService;
import com.leyou.constant.DefineException;
import com.leyou.dao.db1.CouponrecordMapper;
import com.leyou.dao.db2.LeyouCouponsMapper;
import com.leyou.dao.db2.PrscardMapper;
import com.leyou.dao.db2.SmssendcouponsinfoMapper;
import com.leyou.domain.db1entity.ConditionAndGoods;
import com.leyou.domain.db1entity.Couponandactivity;
import com.leyou.domain.db1entity.Couponrecord;
import com.leyou.domain.db3entity.LeyouCoupons;
import com.leyou.domain.db3entity.Prscard;
import com.leyou.domain.db3entity.PrscardExample;
import com.leyou.domain.db3entity.Smssendcouponsinfo;
import com.leyou.domain.vo.CouponActivityInfoVO;
import com.leyou.domain.vo.ReceiveVO;
import com.leyou.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author lixin
 * @Date 2018/11/01 17:16
 * @Version 1.0
 * @Description TODO
 **/
@Service
public class ReceiveServiceImp implements ReceiveService {


    @Autowired
    PrscardMapper prscardMapper;


    @Autowired
    CouponrecordMapper couponrecordMapper;


    @Autowired
    LeyouCouponsMapper leyouCouponsMapper;


    @Autowired
    SmssendcouponsinfoMapper smssendcouponsinfoMapper;


    @Override
    public List<Prscard> getPrscardByPhone(String phone) {

        PrscardExample prscardExample = new PrscardExample();

        prscardExample.or().andMobileEqualTo(String.valueOf(phone));

        List<Prscard> prscardList = prscardMapper.selectByExample(prscardExample);

        return prscardList;

    }

    @Override
    public CouponActivityInfoVO getCouponActivityInfo(String activityKey) {
        return couponrecordMapper.getCouponActivityInfo(activityKey);
    }


    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void putLeyouCoupons(List<LeyouCoupons> leyouCouponsList) {
        int putLeyouCouponsResult = leyouCouponsMapper.putLeyouCoupons(leyouCouponsList);
        if (putLeyouCouponsResult != leyouCouponsList.size()) {
            throw new DefineException(400000, "会员发券数据插入不完全");
        }
    }

    @Override
    public void putSmsSendCoupons(List<Smssendcouponsinfo> smssendcouponsinfoList) {
//        int putSmsSendCouponsResult = smssendcouponsinfoMapper.putSmsSendCoupons(smssendcouponsinfoList);
//        if (putSmsSendCouponsResult != smssendcouponsinfoList.size()) {
//            throw new DefineException(400000, "非会员发券数据插入不完全");
//        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean putReceiveRecord(ReceiveVO receiveVO) {

        CouponActivityInfoVO couponActivityInfoVO = couponrecordMapper.getCouponActivityInfo(receiveVO.getActivitykey());

        int activityResult = couponrecordMapper.updateCouponActivityCount(receiveVO.getActivitykey(), couponActivityInfoVO.getCouponandactivityList().size());

        int activityAndCouponResult = couponrecordMapper.updateCouponAndActivityCount(receiveVO.getActivitykey(), couponActivityInfoVO.getCouponandactivityList());

        List<Couponrecord> couponrecordList = new ArrayList<>();

        for (Couponandactivity couponandactivity : couponActivityInfoVO.getCouponandactivityList()) {
            Couponrecord couponrecord = new Couponrecord(Utils.getUUID(), couponandactivity.getActivitykey(), receiveVO.getActivitykey(), receiveVO.getPhone());
            couponrecordList.add(couponrecord);
        }

        int recordResult = couponrecordMapper.putCouponRecord(couponrecordList);

        if (activityResult != 1 || activityAndCouponResult != couponActivityInfoVO.getCouponandactivityList().size()
                || recordResult != couponActivityInfoVO.getCouponandactivityList().size()) {
            throw new DefineException(400000, "发券记录操作错误" + activityResult + "|" + activityAndCouponResult + "|" + recordResult);
        }

        return false;
    }



    @Override
    public boolean checkSkuList(List<ConditionAndGoods> conditionAndGoodsList) {


        System.out.println(leyouCouponsMapper.selectSkuBySkuList(conditionAndGoodsList)+"-----");

        if (leyouCouponsMapper.selectSkuBySkuList(conditionAndGoodsList) >= conditionAndGoodsList.size()) {
            return true;
        } else {
            return false;
        }
    }

}
